#ifndef _3761_PACKET_H_
#define _3761_PACKET_H_
#include <stdint.h>
#include <stdbool.h>
#include <list>
using namespace std;

/*
 * bit order is like this LSB(Less Sinificant Bit) 
 * bit0;
 * bit1;
 * bit2;
 * bit3;
 * bit4;
 * bit5;
 * bit6;
 * bit7;
 */

#pragma pack(1)
class GB3761_Header
{
public:
	uint8_t  header_Start:8;
	uint16_t header_Version1:2;
	uint16_t header_Length1:14;
	uint16_t header_Version2:2;
	uint16_t header_Length2:14;
	uint8_t  header_Finish:8;
public:
	void initHeader(void);
	void clearHeader(void);
	void setLength(uint16_t);
	bool checkHeader(void)const;
	uint16_t getLength(void)const;
	uint16_t getPacketLength(void)const;
	uint8_t* header(void);
};
class GB3761_Common 
{
public:
	//CONTROL
	uint8_t  control_FUN:4;
	uint8_t  control_FCV:1;//
	uint8_t  control_ACD:1;//as slave ACD, as master FCB
	uint8_t  control_PRM:1;//0=pasive, 1=active
	uint8_t  control_DIR:1;//0=slave,  1=master

	//ADDRESS
	uint16_t address_Region:16;//region
	uint16_t address_Terminal:16;//terminal
	uint8_t  address_Grouping:1; //is group
	uint8_t  address_Maddress:7; //mac address for station

	//Application Function Code
	uint8_t  AFN;

	//Sequnce
	uint8_t sequece_SEQ:4;
	uint8_t sequece_CON:1;//0=no need confirming, 1=need confirming 
	uint8_t sequece_FIN:1;//0=not the end frame, 1=end frame
	uint8_t sequece_FRS:1;//0=not the first frame, 1=first frame
	uint8_t sequece_TPV:1;//TPV=1 AUX{FPC,Date,EC1,EC2}, TPV=0 no AUX
public:
	void initCommon(void);
	void clearCommon(void);
	bool checkCommon(void)const;
	uint8_t* common(void);
};

class PnFn
{
public:
	uint8_t DA1;
	uint8_t DA2;
	uint8_t DT1;
	uint8_t DT2;
public:
	uint16_t getPn(void)const;
	uint8_t  getFn(void)const;
public:
	enum{maxPn=2040,maxFn=148};
};
#pragma pack(0)

#endif//_3761_PACKET_H_
